#include "atr.h"
#include "util.h"
#include "cnfield.h"
#include "cnfield.cpp"
#include "nmfield.h"
#include "nmfield.cpp"
#include "stdffield.h"
#include "stdffield.cpp"

#include "rectype.h"



#include <string>
#include <time.h>
#include <fstream>
#include <assert.h>

using namespace std;
using namespace opsrc;
using namespace opsrc::stdf;

Atr::Atr()
{}

Atr::Atr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex,  endian e, char* buf)
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e)
{
    if (buf != 0)
    {
        readFields(buf);
    }
}

Atr::~Atr()
{
}

StdfField<long> Atr::getModTime() const 
{
    return this->mod_tim;
}

void Atr::setModTime(StdfField<long> value) 
{
    this->mod_tim = value;
}


StdfField<string> Atr::getCmdLine() const 
{
    return this->cmd_lin;
}


void Atr::setCmdLine(StdfField<string> cmdline) 
{
    this->cmd_lin = cmdline;
}

void Atr::readFields(char *buf)
{
#if _DEBUG
    cout << "stdfrec= ATR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", databytecount= " << reclen <<endl;
#endif
    
    int curpos = 0;

    const int lenCnt = 4 + buf[CMD_LINE];
    assert(reclen>=lenCnt);

    // read setup time, 0-3rd bytes
    NmField<unsigned long, 4> tm(buf+curpos, en);
    mod_tim.set(tm.getData(), curpos, tm.getByteCount(), tm.getTotalByteCount());
    curpos += mod_tim.getTotalByteCount();

    if (curpos < reclen -1) {
        CnField<string> cmd(buf + curpos, en);
        cmd_lin.set(cmd.getData(), curpos, cmd.getByteCount(), cmd.getTotalByteCount());
    }
}

